package com.yuchen.ishop.web.admin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuchen.core.MyPager;
import com.yuchen.ishop.service.admin.goods.GoodsService;
import com.yuchen.ishop.service.admin.goods.GoodsTypeService;
import com.yuchen.ishop.web.BaseAction;

/**
 * 商品
 */
@Controller
@RequestMapping(value = "/admin/goods")
public class GoodsAction extends BaseAction {

    private final static Logger LOG = Logger.getLogger(GoodsAction.class);

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/index")
    public String list(ModelMap modelMap) throws Exception {
        return "admin/goods/index";
    }


    //上架 下架
    @RequestMapping(value = "/ex/publish")
    public String audit(Integer publishState, Integer pageNo, ModelMap modelMap, HttpServletRequest request) throws Exception {
        Map map = convertParameterMap(request.getParameterMap());
        String page = "goods_up";
        if (publishState != null && publishState == 0) {
            page = "goods_off";
        }
        map.put("publishState", publishState);
        map.put("state", 1);
        pageNo = pageNo != null ? pageNo : 1;
        MyPager pager = goodsService.findPage(10, pageNo, map);
        modelMap.addAttribute("pager", pager);
        return "admin/goods/" + page;
    }

    @ResponseBody
    @RequestMapping(value = "/ex/recommend")
    public Map recommend( Long id, Long recommend) throws Exception {
        goodsService.recommend(id,recommend);
        return successJson();
    }

    @ResponseBody
    @RequestMapping(value = "/ex/changeState")
    public Map changeState( Long id, Long state) throws Exception {
        goodsService.changeState(id, state);
        return successJson();
    }

    /**
     * 商品审核通过
     *
     * @param modelMap
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/ex/doAuditPass")
    public Object doAuditPass(@RequestParam(value = "ids[]") Long[] ids, ModelMap modelMap) throws Exception {
        goodsService.doAuditPass(ids);
        return successJson();
    }

    /**
     * 商品详情
     *
     * @param modelMap
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/ex/view")
    public String view(Long id, ModelMap modelMap) throws Exception {
        modelMap.addAttribute("good", goodsService.getById(id));
        return "admin/goods/view";
    }


}
